import Vue from 'vue'
import Vuex from 'vuex'
import {getRequest} from "@/utils/api";
import SockJS from  'sockjs-client';
import  Stomp from 'stompjs';
import { Notification } from 'element-ui';

Vue.use(Vuex)

const now = new Date();

const store =  new Vuex.Store({
    state:{
        routes:[],
        sessions:{},
        hrs:[],
        currentSession:null,
        currentHr: JSON.parse(window.sessionStorage.getItem("user")),
        filterKey:'',
        stomp:null,
        Dot:{}
    },
    mutations:{
        initRoutes(state,data){
            state.routes = data;
        },
        changeCurrentSession (state,currentSession) {
            state.currentSession = currentSession;
            Vue.set(state.Dot,state.currentHr.username+'#'+currentSession.username,false);

        },
        addMessage (state,msg) {
            let message = state.sessions[state.currentHr.username+'#'+msg.to];
            if(!message)
            {
                // state.sessions[state.currentHr.username+'#'+msg.to]=[];
                Vue.set(state.sessions,state.currentHr.username+'#'+msg.to,[]);

            }
            state.sessions[state.currentHr.username+'#'+msg.to].push({
                content:msg.content,
                date: new Date(),
                self:!msg.notSelf
            })

        },
        INIT_DATA (state) {
            let data = localStorage.getItem('vue-chat-session');
            // //console.log(data)
            if (data) {
                state.sessions = JSON.parse(data);
            }
        },
        INIT_HR(state,data){
            state.hrs=data;
        },
        INIT_CURRENTHR(state,hr){
            state.currentHr=hr;
        }
    },
    actions:{
        initData (context) {
            context.commit('INIT_DATA')
            getRequest("/chat/hrs").then(resp=>{
                if(resp){
                    context.commit('INIT_HR',resp);
                }
            })
        },
        connect(context){
            context.state.stomp=Stomp.over(new SockJS('/ws/eq'));
            context.state.stomp.connect({},success=>{
                context.state.stomp.subscribe('/user/queue/chat',msg=>{
                    let receiverMsg = JSON.parse(msg.body);
                    if(!context.state.currentHr || receiverMsg.from!=context.state.currentHr.username){
                        Notification.info({
                            title: '['+receiverMsg.fromNickName+']发来一条消息',
                            message: receiverMsg.content.len>10?receiverMsg.content.substr(0,10):receiverMsg.content,
                            position: 'bottom-right'
                        })
                        Vue.set(context.state.Dot,context.state.currentHr.username+'#'+receiverMsg.from,true);
                    }
                    receiverMsg.notSelf = true;
                    receiverMsg.to=receiverMsg.from;
                    context.commit('addMessage',receiverMsg);
                })
            },error=>{})

        }
    }
})


store.watch(function (state) {
    return state.sessions
},function (val) {
    console.log('CHANGE: ', val);
    localStorage.setItem('vue-chat-session', JSON.stringify(val));
},{
    deep:true/*这个貌似是开启watch监测的判断,官方说明也比较模糊*/
})


export default store;